package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.ywj.Title;
import com.bean.ywj.TitleDao;
import com.bean.ywj.TitleDaoImpl;

/**
 * Servlet implementation class DoSaveEditTitle
 */
@WebServlet(
		name="DoSaveEditTitle",
		urlPatterns= {"/servlet/DoSaveEditTitle"},
		loadOnStartup = 1
		)
public class DoSaveEditTitle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoSaveEditTitle() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setContentType("text/html,charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String isbn=request.getParameter("isbn");
		String title=request.getParameter("title");
		String copyright=request.getParameter("copyright");
		String imageFile=request.getParameter("imageFile");
		int editionNumber=Integer.parseInt(request.getParameter("editionNumber"));
		int publisherId=Integer.parseInt(request.getParameter("publisherId"));
		double price=Double.parseDouble(request.getParameter("price"));
		Title titlebean=new Title();
		titlebean.setIsbn(isbn);
		titlebean.setTitle(title);
		titlebean.setCopyright(copyright);
		titlebean.setEditionNumber(editionNumber);
		titlebean.setImageFile(imageFile);
		titlebean.setPrice(price);
		titlebean.setPublisherId(publisherId);
		TitleDao titleDao=new TitleDaoImpl();
		int n=titleDao.update(titlebean);
		if(n>0) {
			request.getRequestDispatcher("/listBook.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
	}

}
